package edu.scut.yi.yx.processer;

import java.sql.SQLException;


import com.electrotank.electroserver5.extensions.api.PluginApi;
import com.electrotank.electroserver5.extensions.api.value.EsObject;
import com.electrotank.electroserver5.extensions.api.value.EsObjectRO;

import edu.scut.yi.yx.dbmanager.DatabaseJDBCPlugin;
import edu.scut.yi.yx.lobbymanager.Command;

public class SetUserInfoProcesser extends AbstractProcesser{
	
	private DatabaseJDBCPlugin databaseJDBCPlugin;
	private EsObject UserinfoResponse;

	@Override
	public String getCommand() {
		// TODO Auto-generated method stub
		return Command.SetUserInfo.getCode();
	}

	@Override
	public void process(String user, EsObjectRO message, PluginApi api) {
		// TODO Auto-generated method stub
		databaseJDBCPlugin = (DatabaseJDBCPlugin)api.getServerPlugin("DatabaseJDBCPlugin");
		
		EsObject param = message.getEsObject(Command.Message.getCode());
		api.getLogger().debug("set user info processer" + param.toString());
		UserinfoResponse = new EsObject();
		UserinfoResponse.setString(Command.Action.getCode(), Command.SetUserInfo.getCode());
		try{
			boolean result = databaseJDBCPlugin.executeSQL("UPDATE userinfo SET gender = "+param.getInteger("gender")+", state = '"+param.getString("state")+"' WHERE username = '"+user+"'");
			api.getLogger().debug("set user info processer result:" + result);
			if(result){
				UserinfoResponse.setString(Command.State.getCode(), Command.Success.getCode());
			}else 
				UserinfoResponse.setString(Command.State.getCode(), Command.Failed.getCode());
		}catch(SQLException e){
			e.printStackTrace();
		}
		api.sendPluginMessageToUser(user, UserinfoResponse);
		
	}

}
